package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxYwxx;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcySqsxYwxxMapper;
import com.yuncheng.spcyApi.service.ISpcySqsxYwxxService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.vo.jcy.HcyXpVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 申请事项-业务信息
 * @Author: yuncheng
 * @Date: 2024-03-06
 * @Version: V1.0
 */
@Service
public class SpcySqsxYwxxServiceImpl extends BaseServiceImpl<SpcySqsxYwxxMapper, SpcySqsxYwxx> implements ISpcySqsxYwxxService {

    public SpcySqsxYwxx addAutoXpryYwxx(HcyXpVo hcyXpVo){

        boolean flag = verifyAutoXpryYwxxIsExists(hcyXpVo.getSqsxid());
        if (flag){
            throw new BusinessException("已经存在正在执行的定时任务！无法再次发起！");
        }

        SpcySqsxYwxx ywxx = new SpcySqsxYwxx();
        ywxx.setId(GetUuIdUtils.ReplaceUuId());
        ywxx.setSqsxid(hcyXpVo.getSqsxid());
        ywxx.setFywflcode(hcyXpVo.getFywflcode());

        ywxx.setZytc(hcyXpVo.getZytc());
        ywxx.setHbqy(hcyXpVo.getHbqy());

        ywxx.setJckssj(hcyXpVo.getKssj());
        ywxx.setJcjssj(hcyXpVo.getJssj());
        ywxx.setFtzsj(hcyXpVo.getFtzsj());

        ywxx.setFzzrs(hcyXpVo.getFzzrs());
        ywxx.setFzyrs(hcyXpVo.getFzyrs());
        ywxx.setFybrs(hcyXpVo.getFybrs());

        ywxx.setTyzzrs(0);
        ywxx.setTyzyrs(0);
        ywxx.setTyybrs(0);

        ywxx.setCxzzrs(0);
        ywxx.setCxzyrs(0);
        ywxx.setCxybrs(0);

        ywxx.setFsmszt(hcyXpVo.getFsmszt());
        ywxx.setFdqrdxnr(hcyXpVo.getFdqrdxnr());

        ywxx.setFstate(SpcyCommonConstant.SQSX_YWXX_STATE_ZXZ);
        save(ywxx);

        return ywxx;
    }

    @Override
    public List<SpcySqsxYwxx> getAutoXpryYwxxList(String sqsxid) {
        return list(new LambdaQueryWrapper<SpcySqsxYwxx>()
                .eq(SpcySqsxYwxx::getSqsxid, sqsxid)
                .orderByDesc(SpcySqsxYwxx::getCreateTime));
    }

    @Override
    public List<SpcySqsxYwxx> getZxzAutoXpryYwxxList(String sqsxid) {
        return list(new LambdaQueryWrapper<SpcySqsxYwxx>()
                .eq(SpcySqsxYwxx::getSqsxid, sqsxid)
                .eq(SpcySqsxYwxx::getFstate, SpcyCommonConstant.SQSX_YWXX_STATE_ZXZ));
    }

    @Override
    public boolean verifyAutoXpryYwxxIsExists(String sqsxid) {
        List<SpcySqsxYwxx> list = this.getZxzAutoXpryYwxxList(sqsxid);
        if (CollectionUtil.isNotEmpty(list)){
            return true;
        }

        return false;
    }

    @Override
    public void updateAutoXpryYwxxByState(String sqsxid) {
        LambdaUpdateWrapper<SpcySqsxYwxx> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(SpcySqsxYwxx::getSqsxid, sqsxid);
        updateWrapper.eq(SpcySqsxYwxx::getFstate, SpcyCommonConstant.SQSX_YWXX_STATE_ZXZ);

        SpcySqsxYwxx updateBean = new SpcySqsxYwxx();
        updateBean.setFstate(SpcyCommonConstant.SQSX_YWXX_STATE_END);
        updateBean.setFjssj(new Date());
        update(updateBean, updateWrapper);
    }

    @Override
    public SpcySqsxYwxx updateAutoXpryTysl(String id, String fssk) {
        if (StringUtils.isBlank(id)){
            return null;
        }
        if (StringUtils.isBlank(fssk)){
            return null;

        }else {
            SpcySqsxYwxx ywxx = getById(id);
            if (ywxx == null){
                return null;
            }

            // 同意人数
            int tyzzrs = ywxx.getTyzzrs();
            int tyzyrs = ywxx.getTyzyrs();
            int tyybrs = ywxx.getTyybrs();

            if (fssk.equals(SpcyConstant.HCY_SSK_ZZ)){
                tyzzrs = tyzzrs + 1;
                this.updateAutoXpryInTyZzrs(id, tyzzrs);
                ywxx.setTyzzrs(tyzzrs);

            }else if (fssk.equals(SpcyConstant.HCY_SSK_ZY)){
                tyzyrs = tyzyrs + 1;
                this.updateAutoXpryInTyZyrs(id, tyzyrs);
                ywxx.setTyzyrs(tyzyrs);

            }else if (fssk.equals(SpcyConstant.HCY_SSK_YB)){
                tyybrs = tyybrs + 1;
                this.updateAutoXpryInTyYbrs(id, tyybrs);
                ywxx.setTyybrs(tyybrs);

            }

            // 满足所有条件结束
            if (ywxx.getFzzrs() == tyzzrs && ywxx.getFzyrs() == tyzyrs && ywxx.getFybrs() == tyybrs){
                LambdaUpdateWrapper<SpcySqsxYwxx> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(SpcySqsxYwxx::getId, id);

                SpcySqsxYwxx updateBean = new SpcySqsxYwxx();
                updateBean.setFstate(SpcyCommonConstant.SQSX_YWXX_STATE_END);
                updateBean.setFjssj(new Date());
                update(updateBean, updateWrapper);

                return ywxx;
            }
        }

        return null;
    }

    public void updateAutoXpryInTyZzrs(String id, int num){
        LambdaUpdateWrapper<SpcySqsxYwxx> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxYwxx::getId, id);

        SpcySqsxYwxx updateBean = new SpcySqsxYwxx();
        updateBean.setTyzzrs(num);
        update(updateBean, updateWrapper);
    }

    public void updateAutoXpryInTyZyrs(String id, int num){
        LambdaUpdateWrapper<SpcySqsxYwxx> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxYwxx::getId, id);

        SpcySqsxYwxx updateBean = new SpcySqsxYwxx();
        updateBean.setTyzyrs(num);
        update(updateBean, updateWrapper);
    }

    public void updateAutoXpryInTyYbrs(String id, int num){
        LambdaUpdateWrapper<SpcySqsxYwxx> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxYwxx::getId, id);

        SpcySqsxYwxx updateBean = new SpcySqsxYwxx();
        updateBean.setTyybrs(num);
        update(updateBean, updateWrapper);
    }
}
